package com.jiezheng.huinong.biz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jiezheng.huinong.dao.ApplyManageDAOImpl;
import com.jiezheng.huinong.dao.IndexManageDAOImpl;
import com.jiezheng.huinong.model.ApplyDetail;
import com.jiezheng.huinong.model.IndexInfo;

public class ExportFile {
	
	/**
	 * 取得所有的申请数据
	 * @param applyNo
	 * @return
	 */
	private static Map<Integer, ArrayList<String>> getApplyData(String applyNo){
		
		Map<Integer, ArrayList<String>> data = new HashMap<Integer, ArrayList<String>>();
		List<ApplyDetail> applyDetails = (new ApplyManageDAOImpl()).getApplyDetailInfo(applyNo);
		
		int lineCount = 0;
		for(ApplyDetail ad: applyDetails){
			ArrayList<String> line = new ArrayList<String>();
			line.add(ad.getAccount()); 		//收款人账号
			line.add(ad.getName());			//收款人名称
			line.add(ad.getBank()); 		//收款人开户行行号 
			line.add(String.valueOf(ad.getMoney())); //金额
			line.add(ad.getAddress());   	//收款人地址
			line.add(ad.getBudgetItem());	//附言
			
			data.put(lineCount, line);
			lineCount++;
		}
		
		return data;
	}

	public static void exportZhiFuFile(String applyNo, String filePath, String fileName) {
		
		// 取得相关的数据
		Map<Integer, ArrayList<String>> data = getApplyData(applyNo);
		
		// 生成相应的文件
		ExcelFileWriter fileWriter = new ExcelFileWriter(filePath, fileName);
		fileWriter.setData(data);

		// 导出到文件
		fileWriter.writeToExcel();

		// 返回
		return;
	}
	
	/**
	 * 取得所有的指标统计数据
	 * @param applyNo
	 * @return
	 */
	private static Map<Integer, ArrayList<String>> getIndexStatisticData(){
		Map<Integer, ArrayList<String>> data = new HashMap<Integer, ArrayList<String>>();
		List<IndexInfo> indexInfos = (new IndexManageDAOImpl()).getStatisticIndexInfo();
		
		// 处理每一行数据
		int lineCount = 0;
		for(IndexInfo info: indexInfos){
			ArrayList<String> line = new ArrayList<String>();
			
			line.add(info.getBudgetUnit());
			line.add(info.getIndexNo());
			line.add(info.getMoney());
			line.add(getUsedMoney(info));
			line.add(info.getSpareMoney());
			line.add(info.getPurpose());
			line.add(info.getBudgetItem());
			
			data.put(lineCount, line);
			lineCount++;	
		}
		
		return data;
	}
	
	public static void exportIndexStatisticFile(String filePath, String fileName) {
		
		// 取得相关的数据
		Map<Integer, ArrayList<String>> data = getIndexStatisticData();
		
		// 生成相应的文件
		IndexStatisticFileWriter fileWriter = new IndexStatisticFileWriter(filePath, fileName);
		fileWriter.setData(data);

		// 导出到文件
		fileWriter.writeToExcel();

		// 返回
		return;
	}
	
	public static String getUsedMoney(IndexInfo indexInfo){
		Double total = Double.valueOf(indexInfo.getMoney());
		Double spareMoney = Double.valueOf(indexInfo.getSpareMoney());
		Double usedMoney = total - spareMoney;
		return String.format("%.2f", usedMoney);
	}
}
